﻿


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Csla.Core;
using Csla.Rules;
using Csla.Rules.CommonRules;

namespace MarketPulse.BusinessLogic.Core.BusinessRules
{
    /// <summary>
    /// Business rule that evaluates a regular expression.
    /// </summary>
    public class ValidGuidRule : CommonBusinessRule
    {

        /// <summary>
        /// Creates an instance of the rule.
        /// </summary>
        /// <param name="primaryProperty">Primary property.</param>
        public ValidGuidRule(Csla.Core.IPropertyInfo primaryProperty)
            : base(primaryProperty)
        {
            InputProperties = new List<IPropertyInfo> { primaryProperty };
        }

        /// <summary>
        /// Rule implementation.
        /// </summary>
        /// <param name="context">Rule context.</param>
        protected override void Execute(RuleContext context)
        {
            var value = context.InputPropertyValues[PrimaryProperty];
            bool ruleSatisfied = false;

            if (value != null && ((Guid?)value).HasValue && ((Guid?)value).Value != Guid.Empty)
            {
                ruleSatisfied = true;
            }

            if (!ruleSatisfied)
            {
                var message = string.Format("Invalid Guid or no Guid set: ", PrimaryProperty.FriendlyName);
                context.Results.Add(new RuleResult(RuleName, PrimaryProperty, message) { Severity = Severity });
            }
        }
    }
}
